package cn.home.DYX.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.home.DYX.service.IHomeDoctorService;
import cn.home.service.domain.HomeServiceRecord;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.home.common.annotation.Log;
import cn.home.common.core.controller.BaseController;
import cn.home.common.core.domain.AjaxResult;
import cn.home.common.enums.BusinessType;
import cn.home.DYX.domain.HomeTeam;
import cn.home.DYX.service.IHomeTeamService;
import cn.home.common.utils.poi.ExcelUtil;
import cn.home.common.core.page.TableDataInfo;

/**
 * 团队管理Controller
 * 
 * @author 丁宇翔
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/system/team")
public class HomeTeamController extends BaseController
{
    @Autowired
    private IHomeTeamService homeTeamService;
    @Autowired
    private IHomeDoctorService homeDoctorService;

    /**
     * 查询团队管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:team:list')")
    @GetMapping("/list")
    public TableDataInfo list(HomeTeam homeTeam)
    {
        startPage();
        List<HomeTeam> list = homeTeamService.selectHomeTeamList(homeTeam);
        return getDataTable(list);
    }

    /**
     * 导出团队管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:team:export')")
    @Log(title = "团队管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HomeTeam homeTeam)
    {
        List<HomeTeam> list = homeTeamService.selectHomeTeamList(homeTeam);
        ExcelUtil<HomeTeam> util = new ExcelUtil<HomeTeam>(HomeTeam.class);
        util.exportExcel(response, list, "团队管理数据");
    }

    /**
     * 获取团队管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:team:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(homeTeamService.selectHomeTeamById(id));
    }
    /*
    * 根据团队id查询团队信息和医生信息以及服务包信息*/
    @PreAuthorize("@ss.hasPermi('system:team:query')")
    @GetMapping("/teamid/{teamId}")
    public AjaxResult getTeamInfo(@PathVariable("teamId") Long teamId){
        //查询团队信息
        HomeTeam homeTeam = homeTeamService.selectHomeTeamById(teamId);
        return success(homeTeam);
    }


    /**
     * 新增团队管理
     */
    @PreAuthorize("@ss.hasPermi('system:team:add')")
    @Log(title = "团队管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HomeTeam homeTeam)
    {
        return toAjax(homeTeamService.insertHomeTeam(homeTeam));
    }

    /**
     * 修改团队管理
     */
    @PreAuthorize("@ss.hasPermi('system:team:edit')")
    @Log(title = "团队管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HomeTeam homeTeam)
    {
        return toAjax(homeTeamService.updateHomeTeam(homeTeam));
    }

    /**
     * 删除团队管理
     */
    @PreAuthorize("@ss.hasPermi('system:team:remove')")
    @Log(title = "团队管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(homeTeamService.deleteHomeTeamByIds(ids));
    }

    /*根据团队id查看团队信息*/
    @PreAuthorize("@ss.hasPermi('system:team:query')")
    @GetMapping("/team/{teamId}")
    public AjaxResult getTeamById(@PathVariable("teamId") Long teamId){
        HomeTeam homeTeam = homeTeamService.selectHomeDoctorByTeamId(teamId);
        return success(homeTeam);
    }

    /*根据团队id添加团队的标签*/
    @PostMapping("team/label")
    public AjaxResult addLabel(int teamId, int ida,int idb,int idc){
        int i = homeTeamService.addLabel(teamId, ida, idb, idc);
        return toAjax(i);
    }
    /*根据团队id添加团队的成员*/
    @GetMapping("team/doctor/team")
    public AjaxResult addDoctor( Long teamId, int doctorIds){
        int i = homeTeamService.addMember(teamId, (long) doctorIds);
        return toAjax(i);
    }
    /*根据团队id添加服务包的id*/
    @GetMapping("team/{teamId}/service")
    public AjaxResult addServicePackage(@PathVariable("teamId") Long teamId,int[] serviceid){
        int i = 0;
        for (int j = 0; j < serviceid.length; j++) {
            i=homeTeamService.addServicePackage(teamId, (long) serviceid[j]);
        }
        return toAjax(i);
    }





}
